package com.syyo.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.common.to.MemberPriceTo;
import com.syyo.common.to.SkuReductionTo;
import com.syyo.gulimall.coupon.domain.entity.MemberPriceEntity;
import com.syyo.gulimall.coupon.domain.entity.SkuFullReductionEntity;
import com.syyo.gulimall.coupon.domain.entity.SkuLadderEntity;
import com.syyo.gulimall.coupon.domain.req.SkuFullReductionReq;
import com.syyo.gulimall.coupon.mapper.SkuFullReductionMapper;
import com.syyo.gulimall.coupon.mapper.SkuLadderMapper;
import com.syyo.gulimall.coupon.service.MemberPriceService;
import com.syyo.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 商品满减信息
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:35:54
 */
@Service
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuFullReductionMapper skuFullReductionMapper;

    @Autowired
    private SkuLadderMapper skuLadderMapper;

    @Autowired
    private MemberPriceService memberPriceService;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(SkuFullReductionReq req) {
        SkuFullReductionEntity entity = new SkuFullReductionEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = skuFullReductionMapper.insert(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = skuFullReductionMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(SkuFullReductionReq req) {
        SkuFullReductionEntity entity = new SkuFullReductionEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = skuFullReductionMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public SkuFullReductionEntity findOne(Integer id) {
        return skuFullReductionMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<SkuFullReductionEntity> findAll(Integer pageNum, Integer pageSize, SkuFullReductionReq req) {
        Page<SkuFullReductionEntity> teacherPage = new Page<SkuFullReductionEntity>(pageNum, pageSize);
        QueryWrapper<SkuFullReductionEntity> wrapper = new QueryWrapper<>();
        return skuFullReductionMapper.selectPage(teacherPage, wrapper);
    }

    /**
     * 远程调用添加
     *
     * @param skuReductionTo
     */
    @Override
    public int feignAdd(SkuReductionTo skuReductionTo) {

        // sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        skuLadderMapper.insert(skuLadderEntity);


        // sku的优惠信息 sms库->   sms_sku_ladder sms_sku_full_reduction sms_member_price
        SkuFullReductionEntity entity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, entity);
        if (entity.getReducePrice().compareTo(new BigDecimal(0)) == 1) {
            int insert = skuFullReductionMapper.insert(entity);
        }

        // sms_member_price
        List<MemberPriceTo> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item -> {
            return item.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);

        return 1;
    }

}